package com.zlx.mall.basic.web.holder;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

/**
 * @author qingke
 **/
@Component
public class MessageSourceHolder implements ApplicationContextAware {

    private static MessageSource messageSource;

    @Override
    public void setApplicationContext(@NotNull ApplicationContext ctx) throws BeansException {
        MessageSourceHolder.messageSource = ctx.getBean(MessageSource.class);
    }

    @NotNull
    public static String getMessage(String code, Object... args) {
        return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
    }


    @NotNull
    public static String getMessage(Locale locale, String code, Object... args) {
        return messageSource.getMessage(code, args, locale);
    }

}
